package weibo4j;

import java.util.List;

import weibo4j.model.Paging;
import weibo4j.model.PostParameter;
import weibo4j.model.Trends;
import weibo4j.model.UserTrend;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;
import weibo4j.util.WeiboConfig;

public class Trend {

	/**
	 * when Weibo service or network is unavailable
	 * 
	 * @version weibo4j-V2 1.0.1
	 * @see <a href="http://open.weibo.com/wiki/2/trends">trends</a>
	 * @since JDK 1.5
	 */
	public List<UserTrend> getTrends(Weibo weibo, String uid) throws WeiboException {
		return UserTrend.constructTrendList(weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "trends.json",
				new PostParameter[] { new PostParameter("uid", uid) }));
	}

	/**
	 * @throws WeiboException
	 *             when Weibo service or network is unavailable
	 * @version weibo4j-V2 1.0.1
	 * @see <a href="http://open.weibo.com/wiki/2/trends">trends</a>
	 * @since JDK 1.5
	 */
	public List<UserTrend> getTrends(Weibo weibo, String uid, Paging page)
			throws WeiboException {
		return UserTrend.constructTrendList(weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "trends.json",
				new PostParameter[] { new PostParameter("uid", uid) }, page));
	}

	/**
	 * when Weibo service or network is unavailable
	 * 
	 * @version weibo4j-V2 1.0.1
	 * @throws JSONException
	 * @see <a
	 *      href="http://open.weibo.com/wiki/2/trends/is_follow">trends/is_follow</a>
	 * @since JDK 1.5
	 */
	public JSONObject isFollow(Weibo weibo, String trend_name) throws WeiboException {
		return weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "trends/is_follow.json",
				new PostParameter[] { new PostParameter("trend_name",
						trend_name) }).asJSONObject();
	}

	/**
	 * @version weibo4j-V2 1.0.1
	 * @throws JSONException
	 * @see <a
	 *      href="http://open.weibo.com/wiki/2/trends/hourly">trends/hourly</a>
	 * @since JDK 1.5
	 */
	public List<Trends> getTrendsHourly(Weibo weibo) throws WeiboException {
		return Trends.constructTrendsList(weibo.getClient().get(WeiboConfig
				.getValue("baseURL") + "trends/hourly.json"));
	}

	public List<Trends> getTrendsHourly(Weibo weibo, Integer base_app) throws WeiboException {
		return Trends.constructTrendsList(weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "trends/hourly.json",
				new PostParameter[] { new PostParameter("base_app", base_app
						.toString()) }));
	}

	/**
	 * @since JDK 1.5
	 */
	public List<Trends> getTrendsDaily(Weibo weibo) throws WeiboException {
		return Trends.constructTrendsList(weibo.getClient().get(WeiboConfig
				.getValue("baseURL") + "trends/daily.json"));
	}

	public List<Trends> getTrendsDaily(Weibo weibo, Integer base_app) throws WeiboException {
		return Trends.constructTrendsList(weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "trends/daily.json",
				new PostParameter[] { new PostParameter("base_app", base_app
						.toString()) }));
	}

	/**
	 * @see <a
	 *      href="http://open.weibo.com/wiki/2/trends/weekly">trends/weekly</a>
	 * @since JDK 1.5
	 */
	public List<Trends> getTrendsWeekly(Weibo weibo) throws WeiboException {
		return Trends.constructTrendsList(weibo.getClient().get(WeiboConfig
				.getValue("baseURL") + "trends/weekly.json"));
	}

	public List<Trends> getTrendsWeekly(Weibo weibo, Integer base_app) throws WeiboException {
		return Trends.constructTrendsList(weibo.getClient().get(
				WeiboConfig.getValue("baseURL") + "trends/weekly.json",
				new PostParameter[] { new PostParameter("base_app", base_app
						.toString()) }));
	}

	/**
	 * @throws JSONException
	 * @see <a
	 *      href="http://open.weibo.com/wiki/2/trends/follow">trends/follow</a>
	 * @since JDK 1.5
	 */
	public UserTrend trendsFollow(Weibo weibo, String trend_name) throws WeiboException {
		return new UserTrend(weibo.getClient().post(WeiboConfig.getValue("baseURL")
				+ "trends/follow.json",
				new PostParameter[] { new PostParameter("trend_name",
						trend_name) }));
	}

	/**
	 * @see <a
	 *      href="http://open.weibo.com/wiki/2/trends/destroy">trends/destroy</a>
	 * @since JDK 1.5
	 */
	public JSONObject trendsDestroy(Weibo weibo, Integer trend_id) throws WeiboException {
		return weibo.getClient().post(
				WeiboConfig.getValue("baseURL") + "trends/destroy.json",
				new PostParameter[] { new PostParameter("trend_id", trend_id
						.toString()) }).asJSONObject();
	}

}
